<template>
  <div class="profile">
    <div class="banner"></div>
    <div class="container wrapper">
      <div class="navbar">
        <profile-info :user="user" @refresh="refresh"/>
        <Card v-if="isSelf" class="links">
          <div class="item">
            <nuxt-link :to="{name:'my-settings'}" class="my-link">
              <div class="icon"><i class="el-icon-edit-outline"></i></div>
              <div class="text">我的资料</div>
              <div class="more"><i class="el-icon-arrow-right"></i></div>
            </nuxt-link>
          </div>
          <div class="item">
            <nuxt-link :to="{name:'my-order'}" class="my-link">
              <div class="icon"><i class="el-icon-tickets"></i></div>
              <div class="text">我的订单</div>
              <div class="more"><i class="el-icon-arrow-right"></i></div>
            </nuxt-link>
          </div>
          <div class="item">
            <nuxt-link :to="{name:'my-invitation'}" class="my-link">
              <div class="icon"><i class="el-icon-chat-line-square"></i></div>
              <div class="text">
                <el-badge :value="unreadCount>0?unreadCount:''" :max="99" class="item">邀请我回答</el-badge>
              </div>
              <div class="more"><i class="el-icon-arrow-right"></i></div>
            </nuxt-link>
          </div>
        </Card>
      </div>
      <div class="content">
        <Posts v-if="isTopic" :topic="topic"></Posts>
        <ProfileComments v-else-if="isAnswer" :comments="comments" :user="user"></ProfileComments>
        <client-only>
          <Order v-if="isOrder"></Order>
          <Settings v-else-if="isSettings"></Settings>
          <Invitation @unreadCount="handlerUnreadCount" v-else-if="isInvitation"></Invitation>
        </client-only>
      </div>
    </div>
  </div>
</template>

<script>
import api from "@/utils/api";
import order from "@/components/order/Order";
import ProfileComments from "@/components/profile/ProfileComments";
import ProfileInfo from "@/components/profile/ProfileInfo";
import Settings from "@/components/profile/Settings";
import Invitation from "@/components/profile/Invitation";

export default {
  name: "test",
  components: {Invitation, order, ProfileInfo, ProfileComments, Settings},

  layout: 'base',
  data() {
    return {
      user: null,
      topic: null,
      comments: null,
      unreadCount: 0
    }
  },
  computed: {
    isTopic() {
      return this.routeName === 'profile-posts' || this.routeName === 'profile-id';
    },
    isAnswer() {
      return this.routeName === 'profile-comments';
    },
    isOrder() {
      return this.routeName === 'my-order';
    },
    isSettings() {
      return this.routeName === 'my-settings';
    },
    isInvitation() {
      return this.routeName === 'my-invitation';
    },
    isSelf() {
      return this.$store.state.user && this.user.id === this.$store.state.user.id;
    },
    id() {
      return this.$route.params.id;
    },
    routeName() {
      return this.$route.name;
    }
  },
  created() {
    let self = this;
    if (self.$store.state.user) {
      api.getUserInvitationCount().then(res => self.unreadCount = res);
    }
  },
  methods: {
    handlerUnreadCount(val) {
      this.unreadCount = val;
    },
    async refresh(userId) {
      let user = await api.getUserDetail(userId);
      this.user = user;
    }
  },
  async asyncData({params, route, store, error}) {
    //判断有没有id，没有就从store中读取
    let userId = params.id;

    if (route.name === 'my-order' || route.name === 'my-settings' || route.name === 'my-invitation') {
      if (!store.state.user) {
        return error({statusCode: 401, message: '请先登录'});
      } else {
        userId = store.state.user.id;
      }
    }

    //读取用户信息
    let user = await api.getUserDetail(userId);


    //读取帖子和评论
    let topic = null;
    if (route.name === 'profile-posts' || route.name === 'profile-id') {
      let query = {};
      query.userId = params.id;
      if (params.page) {
        query.page = params.page
      }
      topic = await api.getTopic(query);
    }
    let comments = null;
    if (route.name === 'profile-comments') {
      comments = await api.getUserComments(params.id, params.page);
    }


    return {
      user, topic, comments
    }
  }
}
</script>

<style scoped lang="less">
.el-badge__content.is-fixed {
  right: 0px;
}

.profile {
  .banner {
    background-image: url("~/static/img/profile_bg.png");
    height: 300px;
    background-size: cover;
  }

  .links {
    .item {
      padding-bottom: 10px;
      margin-bottom: 10px;
      border-bottom: 1px solid var(--border-color);

      &:last-child {
        border-bottom: none;
      }
    }
  }

  .wrapper {
    display: flex;
    margin-top: -100px;

    .navbar {
      flex: 0 0 270px;
    }

    .content {
      flex: 1;
      padding: 10px;
    }
  }
}

.my-link {
  display: flex !important;

  .icon {
    flex: 0 0 30px;
  }

  .text {
    flex: 1;
  }

  .more {
    flex: 0 0 20px;
  }

  .el-icon-arrow-right {
    display: inline-block !important;
  }
}
</style>
